package com.diamond.smart.info.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.diamond.smart.info.entity.DeviceEntity;
import com.diamond.smart.info.service.DeviceService;
import com.diamond.common.utils.PageUtils;
import com.diamond.common.utils.R;


/**
 * @author SunKewei
 * @email skw777jo@163.com
 * @date 2022-04-26 16:02:19
 */
@RestController
@RequestMapping("info/device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    /**
     * 获取所有设备所在的详细地址
     * @return
     */
    @GetMapping("/location")
    public R getDeviceLocation() {
        List<String> locations = deviceService.getDeviceLocation();
        return R.ok().setData(locations);
    }

    /**
     * 根据设备编号查找信息
     */
    @GetMapping("/one/{deviceNum}")
    public R getDeviceByDeviceNum(@PathVariable String deviceNum) {
        DeviceEntity entity = deviceService.getDeviceByDeviceNum(deviceNum);
        return R.ok().put("info", entity);
    }

    /**
     * 根据设备类型查询设备信息
     *
     * @param typeId
     * @return
     */
    @GetMapping("/devices/{id}")
    public R getDevicesByTypeId(@PathVariable("id") Long typeId) {
        List<DeviceEntity> devices = deviceService.getDevicesByTypeId(typeId);
        return R.ok().put("devices", devices);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = deviceService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{deviceId}")
    public R info(@PathVariable("deviceId") Long deviceId) {
        DeviceEntity device = deviceService.getById(deviceId);

        return R.ok().put("device", device);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody DeviceEntity device) {
        deviceService.saveAll(device);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody DeviceEntity device) {
        deviceService.updateAll(device);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] deviceIds) {
        deviceService.removeByIds(Arrays.asList(deviceIds));

        return R.ok();
    }

}
